package de.bastian.hinterleitner.tools;

import java.util.ArrayList;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Point;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;

import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;

import de.hijacksoft.wyc.R;

public abstract class WrapperActivity extends Activity {

	// -------------------------------------------------------
	// Fields
	// -------------------------------------------------------

	private ArrayList<AdView> adViews = new ArrayList<AdView>();
	/**
	 * change this when not developing for HijackSoft Admob Account
	 */
	private static final String AD_UNITID = "a14f5a956778247";

	// -------------------------------------------------------
	// Lifecycle
	// -------------------------------------------------------

	@Override
	protected void onDestroy() {
		for (AdView ad : adViews) {
			ad.destroy();
		}
		super.onDestroy();
	}

	// -------------------------------------------------------
	// Helper Methods
	// -------------------------------------------------------

	protected final String toString(Object[] array) {
		String out = "[ ";
		for (Object str : array) {
			out += str.toString() + ", ";
		}
		out += "]";
		return out;
	}

	public final double roundToDecimals(int decimals, double src) {
		return Math.round((src * Math.pow(10, decimals))) / Math.pow(10, decimals);
	}

	public final String intToMonth(int month) {
		switch (month) {
		case 1:
			return getString(R.string.month01);
		case 2:
			return getString(R.string.month02);
		case 3:
			return getString(R.string.month03);
		case 4:
			return getString(R.string.month04);
		case 5:
			return getString(R.string.month05);
		case 6:
			return getString(R.string.month06);
		case 7:
			return getString(R.string.month07);
		case 8:
			return getString(R.string.month08);
		case 9:
			return getString(R.string.month09);
		case 10:
			return getString(R.string.month10);
		case 11:
			return getString(R.string.month11);
		case 12:
			return getString(R.string.month12);
		default:
			return "" + month;
		}
	}

	public final int monthToInt(String month) {
		if (month.equals(getString(R.string.month01)))
			return 1;
		if (month.equals(getString(R.string.month02)))
			return 2;
		if (month.equals(getString(R.string.month03)))
			return 3;
		if (month.equals(getString(R.string.month04)))
			return 4;
		if (month.equals(getString(R.string.month05)))
			return 5;
		if (month.equals(getString(R.string.month06)))
			return 6;
		if (month.equals(getString(R.string.month07)))
			return 7;
		if (month.equals(getString(R.string.month08)))
			return 8;
		if (month.equals(getString(R.string.month09)))
			return 9;
		if (month.equals(getString(R.string.month10)))
			return 10;
		if (month.equals(getString(R.string.month11)))
			return 11;
		if (month.equals(getString(R.string.month12)))
			return 12;
		return -1;
	}

	// -------------------------------------------------------
	// View-based Methods
	// -------------------------------------------------------

	protected void initAdView() {
		if (!getPackageName().equals("de.hijacksoft.watchyourcash")) {
			View view = findViewById(R.id.adView);
			if (view != null) {
				AdView adView = (AdView) view;
				adView.setVisibility(View.VISIBLE);
				adView.loadAd(new AdRequest());
			}
		}
	}

	/**
	 * sets all options in the given menu to invisible
	 * 
	 * @param m
	 */
	public final void disableAllOptions(Menu m) {
		if (m == null)
			return;
		for (int i = 0; i < m.size(); i++) {
			m.getItem(i).setVisible(false);
		}
	}

	/**
	 * sets all options in the given menu to visible
	 * 
	 * @param m
	 */
	public final void enableAllOptions(Menu m) {
		if (m == null)
			return;
		for (int i = 0; i < m.size(); i++) {
			m.getItem(i).setVisible(true);
		}
	}

	// -------------------------------------------------------
	// Animation Methods
	// -------------------------------------------------------

	protected void slideInFromTop(final View v, long duration) {
		v.startAnimation(computeSlidingAnimation(new Point(0, -v.getHeight()),
				new Point(0, 0), duration));
	}

	protected void slideInFromRight(final View v, long duration) {
		v.startAnimation(computeSlidingAnimation(new Point(v.getWidth(), 0),
				new Point(0, 0), duration));
	}

	protected void slideInFromBottom(final View v, long duration) {
		v.startAnimation(computeSlidingAnimation(new Point(0, v.getHeight()),
				new Point(0, 0), duration));
	}

	protected void slideInFromLeft(final View v, long duration) {
		v.startAnimation(computeSlidingAnimation(new Point(-v.getWidth(), 0),
				new Point(0, 0), duration));
	}

	private Animation computeSlidingAnimation(Point from, Point to,
			long duration) {
		AnimationSet animation = new AnimationSet(true);

		TranslateAnimation translateAnimation = new TranslateAnimation(from.x,
				to.x, from.y, to.y);
		translateAnimation.setDuration(duration);
		animation.addAnimation(translateAnimation);

		return animation;
	}

	// -------------------------------------------------------
	// Dialogs
	// -------------------------------------------------------

	public final void dialog(String text) {
		dialog(null, text);
	}

	public final void dialog(String title, String text) {
		final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		if (title != null)
			alertDialog.setTitle(title);
		alertDialog.setMessage(text);
		alertDialog.setCanceledOnTouchOutside(true);
		alertDialog.setButton(getString(R.string.ok),
				new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						alertDialog.cancel();
					}
				});
		alertDialog.show();
	}

	public final void dialog(String title, View view) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setTitle(title);
		alertDialog.setView(view);
		alertDialog.show();
	}

	public final void question(String text, String yesText, String noText,
			OnClickListener positive, OnClickListener negative) {
		AlertDialog alertDialog = new AlertDialog.Builder(this).create();
		alertDialog.setMessage(text);
		alertDialog.setButton(yesText, positive);
		alertDialog.setButton2(noText, negative);
		alertDialog.show();
	}
}
